package com.LeeCode;

/**
 * 特殊数组 II
 */

public class Code3152 {
    public static void main(String[] args) {

    }

    public boolean[] isArraySpecial(int[] nums, int[][] queries) {
        int n = nums.length;
        // 计算前缀和
        int[] prefixSum = new int[n];
        for (int i = 1; i < n; i++) {
            int add = (nums[i] & 1) == (nums[i - 1] & 1) ? 1 : 0;
            prefixSum[i] = prefixSum[i - 1] + add;
        }

        // 处理查询
        boolean[] answer = new boolean[queries.length];
        for (int i = 0; i < queries.length; i++) {
            int from = queries[i][0];
            int to = queries[i][1];

            if (from == to) {
                answer[i] = true;
            } else {
                // 检查区间[from, to-1]内是否有bad位置
                int badCount = prefixSum[to] - prefixSum[from];
                answer[i] = (badCount == 0);
            }
        }

        return answer;
    }
}
